package com.lagou.demo;

import com.lagou.dao.UserMapper;
import com.lagou.dao.impl.UserMapperImpl;
import com.lagou.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest {

    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        //加载核⼼配置⽂件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //获得sqlSession⼯⼚对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得sqlSession对象
        sqlSession = sqlSessionFactory.openSession();// 默认开启事务,但不会自动提交
    }

    @After
    public void after(){
        sqlSession.close();
    }

    @Test
    public void select()  {
        List<User> users = sqlSession.selectList("com.lagou.dao.UserMapper.findAll");
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void findByCondition()  {
        User user = new User();
        //user.setId(2);
        user.setUsername("Jon");
        user = sqlSession.selectOne("com.lagou.dao.UserMapper.findByCondition", user);
        System.out.println(user);
    }

    @Test
    public void insert()  {
        User user = new User();
        user.setId(3);
        user.setUsername("Jon");
        user.setPassword("123");
        user.setBirthday("1987-08-03");
        int insert = sqlSession.insert("com.lagou.dao.UserMapper.add", user);
        sqlSession.commit();// 需要提交
        System.out.println("插入" + insert + "条数据成功!");
        List<User> users = sqlSession.selectList("com.lagou.dao.UserMapper.findAll");
        users.forEach(user1 -> System.out.println(user1));
    }

    @Test
    public void update()  {
        User user = new User();
        user.setId(3);
        user.setUsername("kitty");
        user.setPassword("234");
        int update = sqlSession.update("com.lagou.dao.UserMapper.update", user);
        sqlSession.commit();// 需要提交
        System.out.println("更新" + update + "条数据成功!");
        List<User> users = sqlSession.selectList("com.lagou.dao.UserMapper.findAll");
        users.forEach(user1 -> System.out.println(user1));
    }

    @Test
    public void delete()  {
        int delete = sqlSession.delete("com.lagou.dao.UserMapper.delete", 3);
        sqlSession.commit();// 需要提交
        System.out.println("删除" + delete + "条数据成功!");
        List<User> users = sqlSession.selectList("com.lagou.dao.UserMapper.findAll");
        users.forEach(user1 -> System.out.println(user1));
    }

    @Test
    public void findAllClassic() throws IOException {
        // 传统开发方式方式
        UserMapperImpl userDao = new UserMapperImpl();
        List<User> users = userDao.findAll();
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void findAllByProxy() throws IOException {
        // 代理开发方式
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.findAll();
        users.forEach(user -> System.out.println(user));
    }
}
